﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PhanCongGiangDay
{
    public class CBoMon
    {
        public static List<MBoMon> getAll()
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            var bms = from bm in db.BOMONs
                      select new MBoMon
                      {
                          MABM = bm.MABM,
                          TENBM = bm.TENBM
                      };
            return bms.ToList();
        }

        public static int addBM(string maBM, string tenBM)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                BOMON bm = new BOMON
                {
                    MABM = maBM,
                    TENBM = tenBM
                };
                db.BOMONs.InsertOnSubmit(bm);
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static int updateBM(string maBM, string tenBM)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                BOMON bm = (from bmObject in db.BOMONs
                                where bmObject.MABM == maBM
                                select bmObject).First();
                //bm.MABM = maBM;
                bm.TENBM = tenBM;
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static int deleteBM(string maBM)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                BOMON bm = db.BOMONs.Where(b => b.MABM == maBM).Single();
                db.BOMONs.DeleteOnSubmit(bm);
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }        
    }
}
